package com.cmnit.utils;

import com.cmnit.bean.DangerousTruckBean;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * oracle工具类
 *
 * @author Chan
 * @version 1.0
 * @date 2019/7/2
 */
public class OracleUtils {
    private static final Logger log = Logger.getLogger(OracleUtils.class);

    /**
     * 将日志数据插入数据库
     *
     * @param tableName 表名
     */
    public static void insert(String tableName, List<String> list, String fileName) {
        Connection conn = JDBCUtils.getConnection();
        try {
            conn.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PreparedStatement pst = null;
        try {
            int size = fileName.split(",").length;
            String sql = "INSERT INTO " + tableName + " VALUES (";
            for (int i = 1; i < size; i++) {
                sql = sql + "?,";
            }
            sql = sql + "?)";
            log.info("sql:" + sql);
            pst = conn.prepareStatement(sql);

            for (String line : list) {
                String[] info = line.split(",");
                for (int i = 0; i < size; i++) {
                    pst.setString(i + 1, info[i]);
                }
                pst.addBatch();
            }
            pst.executeBatch();
            conn.commit();
        } catch (SQLException e) {
            log.error("数据入库失败：" + e.getMessage());
        } finally {
            try {
                JDBCUtils.close(conn, pst);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除数据
     *
     * @param tableName 表名
     */
    public static void delete(String tableName) {
        String sql = "delete from " + tableName;
        log.info("sql:" + sql);
        PreparedStatement pstmt = null;
        Connection connect = JDBCUtils.getConnection();
        try {
            pstmt = connect.prepareStatement(sql);
            pstmt.execute();
            log.info("删除" + tableName + "表数据");
        } catch (SQLException e) {
            log.error("删除失败：" + e.getMessage());
        } finally {
            JDBCUtils.close(connect, pstmt);
        }
    }

    /**
     * 删除数据
     *
     * @param tableName 表名
     * @param acctName  时间字段
     * @param acctDate  账期
     */
    public static void delete(String tableName, String acctName, String acctDate) {
        String sql = "delete from " + tableName + " where " + acctName + " = ?";
        log.info("sql:" + sql);
        PreparedStatement pstmt = null;
        Connection connect = JDBCUtils.getConnection();
        try {
            pstmt = connect.prepareStatement(sql);
            pstmt.setString(1, acctDate);
            pstmt.execute();
            log.info("删除" + tableName + "表的账期：" + acctDate);
        } catch (SQLException e) {
            log.error("删除失败：" + e.getMessage());
        } finally {
            JDBCUtils.close(connect, pstmt);
        }
    }
}
